﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration.Provider;
using System.Collections.Specialized;
using System.Reflection;
using System.Web.Configuration;

namespace PictureManager.Core.Provider
{
    public static class PictureService
    {
        #region Constants and Fields
        /// <summary>
        /// The lock object.
        /// </summary>
        private static readonly object lockObj = new object();
       
        private static PictureProvider _provider;
        private static PictureProviderCollection _providers;
        #endregion

        #region Properties

        /// <summary>
        ///     Gets the current provider.
        /// </summary>
        public static PictureProvider Provider
        {
            get
            {
                LoadProviders();
                return _provider;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Load the providers.
        /// </summary>
        private static void LoadProviders()
        {
            // Avoid claiming lock if providers are already loaded
            if (_provider == null)
            {
                lock (lockObj)
                {
                    // Do this again to make sure _provider is still null
                    if (_provider == null)
                    {
                        _provider = new XmlProvider();
                        //// Get a reference to the <PictureProvider> section
                        //try
                        //{
                        // always find the dll from exe path??????
                        //    var section = (PictureProviderSection)ConfigurationManager.Instance.GetSection("PictureProvider");
                        //    _providers = new PictureProviderCollection();
                        //    ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(PictureProvider));

                        //    _provider = _providers[section.DefaultProvider];

                        //    if (_provider == null)
                        //    {
                        //        throw new ProviderException("Unable to load default BlogProvider");
                        //    }
                        //}
                        //catch (Exception ex)
                        //{
                        //    UtilLib.Util.Log(UtilLib.LogLevel.Error, ex);
                        //}
                    }
                }
            }
        }

        #region Settings

        /// <summary>
        /// Save the settings to the current provider.
        /// </summary>
        /// <param name="settings">
        /// The settings.
        /// </param>
        public static void SaveSettings(StringDictionary settings)
        {
            Provider.SaveSettings(settings);
        }

        /// <summary>
        /// Loads the settings from the provider and returns
        /// them in a StringDictionary for the BlogSettings class to use.
        /// </summary>
        /// <returns>A StringDictionary.</returns>
        public static StringDictionary LoadSettings()
        {
            return Provider.LoadSettings();
        }

        public static string GetStorageLocation()
        {
            return Provider.StorageLocation();
        }
        public static string GetPictureRootNode()
        {
            return Provider.PictureRootNode();
        }

        public static string GetBackupLocation()
        {
            return Provider.BackupLocation();
        }
        #endregion

        #region Picture
        /// <summary>
        /// Inserts a new <see cref="PictureItem"/> into the data store specified by the provider.
        /// </summary>
        /// <param name="item">
        /// The picture Item to insert.
        /// </param>
        public static void InsertPictureItem(PictureItem item)
        {
            Provider.InsertPictureItem(item);
        }
        /// <summary>
        /// Deletes a <see cref="PictureItem"/> from the data store specified by the provider.
        /// </summary>
        /// <param name="item">
        /// The <see cref="PictureItem"/> to delete.
        /// </param>
        public static void DeletePictureItem(PictureItem item)
        {
            Provider.DeletePictureItem(item);
        }
        /// <summary>
        /// Updates an existing <see cref="PictureItem"/> in the data store specified by the provider.
        /// </summary>
        /// <param name="item">
        /// The <see cref="PictureItem"/> to update.
        /// </param>
        public static void UpdatePictureItem(PictureItem item)
        {
            Provider.UpdatePictureItem(item);
        }
        /// <summary>
        /// Retrieves all picture items from the provider and returns them in a list.
        /// </summary>
        /// <returns>A list of <see cref="PictureItem"/>.</returns>
        public static List<PictureItem> GetPictures()
        {
            return Provider.GetPictures();
        }

        /// <summary>
        /// Gets a <see cref="PictureItem"/> based on a id.
        /// </summary>
        /// <param name="id">
        /// The <see cref="PictureItem"/>'s id.
        /// </param>
        /// <returns>
        /// A matching <see cref="PictureItem"/>
        /// </returns>
        public static PictureItem GetPictureItem(String id)
        {
            return Provider.GetPictureItem(id);
        }

        public static void SavePictureList()
        {
            Provider.SavePictureList();
        }
        #endregion

        #endregion
    }
}
